﻿using fuzhu;
using LH.Model.PU;
using LH.Models.APAR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NETCore.MailKit.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Ocsp;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using U8COWeb;
using XXX.api.Controllers;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.AS;
using XXX.Models.GL;
using Yonyou.U8.BO; 

namespace Yonyou.U8.WebApi.Controllers
{
    [Route("api/APVouch/[action]")]
    [ApiController]
    [Authorize]
    public class ApiAPVouchController : ControllerBase
    {
        public static readonly object _lockObj = new object();
        private readonly ILogger<ApiAPVouchController> _logger;
        private readonly IEmailService _EmailService;

        public ApiAPVouchController(ILogger<ApiAPVouchController> logger, IEmailService emailService)
        {
            _logger = logger;
            _EmailService = emailService;
        }

        /// <summary>
        /// 付款单写入到U8,一张单据调用一次
        /// </summary> 
        [HttpPost]
        public RetModel2 AddVerify(JObject JsonObj)
        {
            //一次只执行一个线程
            //lock (_lockObj)
            //{
            string requestBody = DbHelper.GetDbString(JsonObj);
            try
                {

                string cErr = "";
                string cCode = "";
                Api_APVouch api_entity;
                try
                {
                    api_entity = JsonConvert.DeserializeObject<Api_APVouch>(requestBody);
                    if (api_entity == null)
                    {
                        cErr = "json结构异常!空值";
                        return null;
                    }
                }
                catch (Exception e)
                {
                    throw new Exception("json结构异常！" + e.Message);
                }
                if (string.IsNullOrWhiteSpace(api_entity.cVouchID))
                {
                    cErr = "委外发票号不能为空";
                    return null;
                }
                APVouchVM model = new APVouchVM(new APVouchVM.Head(), new List<APVouchVM.Body>());

                model = model.GetAddEntity(api_entity, ref cErr, ref cCode);
                RetModel2 ret;
                if (model == null)
                {
                    ret = DBService.ReErrObject("入参有误" + cErr);
                }
                else
                {


                    ret = model.Add();
                    //model.conStr = CanShu.conStr;                   

                }
                
                // 将 JSON 数据保存到数据库中
                JsonSaveService.saveLog2("应付单", api_entity.cVouchID, requestBody, ret.code, ret.message);
                //new SendEmailService(_EmailService, _logger).Send(CanShu.mail.SendCope, ret.code, CanShu.mail.ToAddress, vm.accid + "应付单," + vm.cVouchID, "报文:\r\n" + requestBody + "\r\n" + "结果,\r\n" + ret.message);


                return ret; 

                }




                catch (Exception ex)
            {
                new SendEmailService(_EmailService, _logger).Send(CanShu.mail.SendCope, 500, CanShu.mail.ToAddress,  "应付单," , "报文:\r\n" + requestBody + "\r\n" + "结果,\r\n" + ex.Message);
                return DBService.ReErrObject(ex.Message);
            }
            //}


        }

        [HttpPost]
        public RetModel2 Delete(JObject obj)
        {
            string requestBody = DbHelper.GetDbString(obj);
            if (requestBody == null)
            {
                return DBService.ReErrObject("入参有误，没有数据");
            }

            try
            {
                string cErr = "";
                Api_APVouch api_entity;
                try
                {
                    api_entity = JsonConvert.DeserializeObject<Api_APVouch>(requestBody);
                    if (api_entity == null)
                    {
                        cErr = "json结构异常!空值";
                        return null;
                    }
                }
                catch (Exception e)
                {
                    throw new Exception("json结构异常！" + e.Message);
                }
                APVouchVM model = new APVouchVM(new APVouchVM.Head(), new List<APVouchVM.Body>());

                model = model.GetCodeEntity(api_entity, ref cErr);
                RetModel2 ret;
                if (model == null)
                {
                    ret = DBService.ReErrObject(cErr);
                }
                else
                {
                    ret = model.Delete();
                }
                // 将 JSON 数据保存到数据库中
                JsonSaveService.saveLog2("委外发票弃审删除", model.head.cVouchID, requestBody, ret.code, ret.message);


                return ret;
            }
            catch (Exception ex)
            {
                //_EmailService.Send(CanShu.mail.ToAddress, CanShu.cZT + "销售订单弃审删除,程序错误：", "报文:\r\n" + requestBody + "\r\n" + "结果：\r\n" + ex.Message);
                return DBService.ReErrObject(ex.Message);
            }

        }

    }
}



